#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: send_beanstalk_compare.py
@time: 2018/1/21 13:22
"""

import sys
import time

sys.path.append('..')
sys.path.append('../..')

from common import util
from common.mongo import MongDb
from config.beanstalk_conf import BEANSTALK_CONF
from config.mongo_conf import MONGO_DB_SOURCE
from ext.beanstalk_handler import BeanstalkHandler
from logger import Logger

# 日志模块
global_log = Logger('send_zhejiang_beanstalk.log').get_logger()


class SendBeanstalk(object):
    MONGO_CONF = {
        'host': '172.16.215.16',
        'port': 40042,
        'db': 'app_data',
        'username': 'work',
        'password': 'haizhi'
    }

    def __init__(self, beanstalk_conf, tube, _log):
        self.log = _log

        # 初始化消息队列
        self.beanstalk_thread = BeanstalkHandler(beanstalk_conf=beanstalk_conf, log=_log)
        self.beanstalk_thread.start()

        # 初始化mongodb
        self.source_db = MongDb(MONGO_DB_SOURCE['host'], MONGO_DB_SOURCE['port'], MONGO_DB_SOURCE['db'],
                                MONGO_DB_SOURCE['username'],
                                MONGO_DB_SOURCE['password'], log=_log)
        # 宁波库
        self.app_data_db = MongDb(self.MONGO_CONF['host'], self.MONGO_CONF['port'], self.MONGO_CONF['db'],
                                  self.MONGO_CONF['username'],
                                  self.MONGO_CONF['password'], log=_log)
        self.app_data_table = 'zhejiang_ningbo_2017'
        self.source_table = 'new_zhejiang_company_list'

        self.tube = tube

    def start(self):

        while True:
            is_all_finish = True
            for item in self.app_data_db.traverse_batch_field(self.app_data_table, {}, ['_id']):
                company = item.get('_id')
                result_item = self.source_db.find_one(self.source_table, {'_id': company})
                if result_item is None:
                    self.source_db.insert_batch_data(self.source_table, [{
                        '_id': company,
                        '_in_time': util.get_now_time(),
                        'search_status': 0,
                        'crawl_status': 0
                    }])

                    self.beanstalk_thread.push_msg(self.tube, {
                        'province': 'zhejiang',
                        'company': company,
                        'seed_table': self.source_table,
                    })
                    is_all_finish = False
                    self.log.info("发送企业信息到消息队列: company:{} ".format(company))
                    continue
                crawl_status = result_item.get('crawl_status')
                if crawl_status == 1:
                    continue

                search_status = result_item.get('search_status')
                if search_status == 2:
                    continue

                self.beanstalk_thread.push_msg(self.tube, {
                    'province': 'zhejiang',
                    'company': company,
                    'seed_table': self.source_table,
                })
                is_all_finish = False
                self.log.info("发送企业信息到消息队列: company:{} ".format(company))

            if is_all_finish:
                self.log.info("所有企业抓取成功!")
                break
            time.sleep(300)


def main():
    SendBeanstalk(BEANSTALK_CONF, tube='zhejiang_list_download', _log=global_log).start()


if __name__ == '__main__':
    main()
